package cn.fl.risknew.controller;

import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.ref.fribaseinfo.vo.FriBaseInfoVO;
import cn.fl.risknew.condition.RskUsrOrgRoleQC;
import cn.fl.risknew.service.IAutoAssignService;
import cn.fl.risknew.service.IRiskOverdueService;
import cn.fl.risknew.service.IRskUsrCstRNewService;
import cn.fl.risknew.vo.AutoAssignResultVO;
import cn.fl.risknew.vo.RskUsrCstRVO;
import com.alibaba.dubbo.container.page.PageServlet;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 任务分配
 * 作者:hongyanbo
 * 时间:2018/9/10
 */
@Controller
@RequestMapping("/risknew/rskUsrCstR/autoAssign")
public class AutoAssignTaskController {
    /**
     * 自动分配服务
     */
    @Autowired
    private IAutoAssignService autoAssignService;
    @Autowired
    private IRskUsrCstRNewService rskUsrCstRNewService;
    @Autowired
    private IRiskOverdueService overdueService;
    /**
	 * 客户 服务层BEAN
	 */
	@Autowired
	private ICstBscInfoService cstBscInfoService;//客户基础信息表
    /**
     * 到输入金额页面
     * @param view
     * @param age age=0:短账龄  age=1 中长账龄
     * @return
     */
    @RequestMapping("/toMoneyInput.do")
    public ModelAndView toMoneyInput(ModelAndView view,int age){
        view.setViewName("/risknew/rskUsrCstR/autoassign/moneyInput");
    	// 事业部
		List<KeyValueVO> orgList = this.cstBscInfoService
				.searchBusinessOrgList();
        List<FriBaseInfoVO> list = this.overdueService.getFriBaseInfoList();
        if(CollectionUtils.isNotEmpty(list)){
            List<KeyValueVO> insList = new ArrayList<>();
            for (FriBaseInfoVO friBaseInfoVO : list) {
                KeyValueVO vo = new KeyValueVO();
                vo.setKey(friBaseInfoVO.getInsCode());
                vo.setValue(friBaseInfoVO.getFundRasInsNm());
                insList.add(vo);
            }
            view.addObject("insList", JSON.toJSONString(insList));
        }
		view.addObject("orgListList", JSON.toJSONString(orgList));
        view.addObject("age",age);
        return view;
    }
    /**
     * 到人员选择页面
     * @param view
     * @param age
     * @param money
     * @return
     */
    @RequestMapping("/toUsrList.do")
    public ModelAndView toUsrList(ModelAndView view,int age,Double money,String buOrgCd,String ovdDaysMin,String ovdDaysMax,String mcrCd,String bizFlag,String insCode){
        // 计算
        AutoAssignResultVO resultVO = null;
        if(age == 0){ //短账龄
            List<String> buOrgList = null;
            if(StringUtils.isNotBlank(buOrgCd)){
                buOrgList = Arrays.asList(buOrgCd.split(";"));
            }
            List<String> insCdList = null;
            if(StringUtils.isNotBlank(insCode)){
                insCdList= Arrays.asList(insCode.split(";"));
            }
            resultVO = this.autoAssignService.getShortAgeCstList(money,buOrgList,ovdDaysMin,ovdDaysMax,mcrCd,bizFlag,insCdList);
            
        }else{ //长账龄
            resultVO = this.autoAssignService.getLongAgeCstList(money);
        }
        view.addObject("sum", DecimalUtils.convertFenToYuan(resultVO.getSum()));
        view.addObject("cstNum",resultVO.getCstIdList().size());
        view.addObject("ids", JSON.toJSONString(resultVO.getCstIdList()));
        view.setViewName("/risknew/rskUsrCstR/autoassign/usrList");
        view.addObject("age",age);
        return view;
    }

    /**
     * 中长账龄分配任务人
     * 中长账龄只分配给贷后人员
     * @param page
     * @param pagesize
     * @return
     */
    @RequestMapping("initLongAgeCstList.do")
    @ResponseBody
    public ListPageVO initLongAgeUsrList (int page,int pagesize,RskUsrOrgRoleQC qc){
        Pager pager = new Pager();
        pager.setCurrentPage(page);
        pager.setEveryPage(pagesize);
        ArrayList<String> roleIds = new ArrayList<>();
        roleIds.add("1809120001"); //重卡自动派单
        roleIds.add("1809120002"); //汽车金融自动派单
        qc.setRoleIdList(roleIds);
        pager.setCondition(qc);
        pager = this.rskUsrCstRNewService.searchUsrList(pager);
        List<RskUsrCstRVO> resultList = (List<RskUsrCstRVO>) pager.getResultList();
        ListPageVO<RskUsrCstRVO> listPageVO = new ListPageVO<>(resultList, pager.getTotalCount());
        return listPageVO;
    }

    /**
     * 短账龄分配任务人列表
     * 短账龄只分配给 信审
     * @param page
     * @param pagesize
     * @param qc
     * @return
     */
    @RequestMapping("initShortAgeCstList.do")
    @ResponseBody
    public ListPageVO<RskUsrCstRVO> initShortAgeUsrList (int page, int pagesize, RskUsrOrgRoleQC qc){
        Pager pager = new Pager();
        pager.setCurrentPage(page);
        pager.setEveryPage(pagesize);
        ArrayList<String> roleIds = new ArrayList<>();
        roleIds.add("10030"); //信审员
        qc.setRoleIdList(roleIds);
        pager.setCondition(qc);
        pager = this.rskUsrCstRNewService.searchUsrList(pager);
        List<RskUsrCstRVO> resultList = (List<RskUsrCstRVO>) pager.getResultList();
        ListPageVO<RskUsrCstRVO> listPageVO = new ListPageVO<>(resultList, pager.getTotalCount());
        return listPageVO;
    }
}
